/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lvoddownloader;

import LPlayerUILib.JImagePanel;
import java.io.File;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.tree.DefaultTreeCellRenderer;

/**
 *
 * @author wcss
 */
public class LCurrentPlayList extends JImagePanel {

    public ArrayList<PlayListItem> currentPlayList = new ArrayList<PlayListItem>();
    public PlayListActionInterface playAction = null;
    public PlayListItem currentPlayItem = null;
    public PlayListItem selectedItem = null;

    /**
     * Creates new form LCurrentPlayList
     */
    public LCurrentPlayList() {
        initComponents();
        //this.btnOpen.setVisible(false);
    }

    public void savePlayList() {
        try {
            File save = new File(JAppToolKit.JRunHelper.getUserHomeDirPath() + "/playinglist.dat");
            String[] team = new String[currentPlayList.size()];
            int index = 0;
            for (PlayListItem pli : currentPlayList) {
                team[index] = pli.moviePath;
                index++;
            }
            JAppToolKit.JDataHelper.writeAllLines(save.getAbsolutePath(), team);
        } catch (Exception ex) {
            Logger.getLogger(LCurrentPlayList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void loadPlayList() {
        this.currentPlayList.clear();
        File f = new File(JAppToolKit.JRunHelper.getUserHomeDirPath() + "/playinglist.dat");
        if (f.exists()) {
            try {
                String[] contents = JAppToolKit.JDataHelper.readAllLines(f.getAbsolutePath());
                for (String s : contents) {
                    this.currentPlayList.add(new PlayListItem(s));
                    System.out.println(s);
                }
            } catch (Exception ex) {
                Logger.getLogger(LCurrentPlayList.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        this.jlMovieList.setListData(this.currentPlayList.toArray());
    }

    public void addMovie(String mp) {
        this.currentPlayList.add(new PlayListItem(mp));
        savePlayList();
        loadPlayList();
    }

    public void addMovieFromDirectory(String dir)
    {
        File dirf = new File(dir);
        if (dirf.exists())
        {
            File[] files = dirf.listFiles();
            for(File f : files)
            {
                addMovie(f.getAbsolutePath());
            }
        }
    }
    
    /**
     * Creates new form LCurrentPlayList
     */
    public LCurrentPlayList(String image) {
        initComponents();
        try {
            this.backgroundImageUrl = LCurrentPlayList.class.getResource(image);
            this.backgroundImageObj = new ImageIcon(this.backgroundImageUrl);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        plButtons = new LPlayerUILib.JImagePanel("/UIImages/video/bg-1.png");
        btnDel = new LPlayerUILib.JImageButton("/UIImages/video/del.png","/UIImages/video/del.png","/UIImages/video/del.png","",true,true);
        spTreePane = new javax.swing.JScrollPane();
        jlMovieList = new javax.swing.JList();

        setBorder(null);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });
        setLayout(null);

        plButtons.setBackground(new java.awt.Color(146, 140, 135));
        plButtons.setLayout(null);

        btnDel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnDelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout btnDelLayout = new javax.swing.GroupLayout(btnDel);
        btnDel.setLayout(btnDelLayout);
        btnDelLayout.setHorizontalGroup(
            btnDelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 11, Short.MAX_VALUE)
        );
        btnDelLayout.setVerticalGroup(
            btnDelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 11, Short.MAX_VALUE)
        );

        plButtons.add(btnDel);
        btnDel.setBounds(230, 10, 11, 11);

        add(plButtons);
        plButtons.setBounds(0, 0, 255, 27);

        spTreePane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        jlMovieList.setBackground(new java.awt.Color(44, 45, 50));
        jlMovieList.setBorder(null);
        jlMovieList.setForeground(java.awt.Color.white);
        jlMovieList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jlMovieListMouseClicked(evt);
            }
        });
        spTreePane.setViewportView(jlMovieList);

        add(spTreePane);
        spTreePane.setBounds(0, 30, 250, 440);
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        // TODO add your handling code here:
        this.spTreePane.setLocation(0, this.plButtons.getHeight());
        this.spTreePane.setSize(this.getWidth(), this.getHeight() - this.plButtons.getHeight());
    }//GEN-LAST:event_formComponentResized

    private Boolean isNeedDelete(int[] selects,int index)
    {
        Boolean result = false;
        for(int f: selects)
        {
           if (f == index)
           {
               result = true;
               break;
           }   
        }        
        return result;
    }
    
    private void btnDelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnDelMouseClicked
        // TODO add your handling code here:
        int[] sels = this.jlMovieList.getSelectedIndices();
        if (sels != null && sels.length > 0)
        {
            ArrayList<PlayListItem> temp = new ArrayList<PlayListItem>();
            for(int j = 0;j < this.currentPlayList.size();j++)
            {
                if (!isNeedDelete(sels,j))
                {
                    temp.add(currentPlayList.get(j));
                }
            }
            currentPlayList.clear();
            currentPlayList.addAll(temp);            
            this.savePlayList();
            this.loadPlayList();
        }
    }//GEN-LAST:event_btnDelMouseClicked

    private void jlMovieListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jlMovieListMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() == 1) {
            if (this.jlMovieList.getSelectedValue() != null) {
                selectedItem = (PlayListItem) this.jlMovieList.getSelectedValue();
            }
        } else {
            if (this.jlMovieList.getSelectedValue() != null) {
                selectedItem = (PlayListItem) this.jlMovieList.getSelectedValue();
                currentPlayItem = selectedItem;
                if (this.playAction != null)
                {
                    this.playAction.runAction(this, currentPlayItem.moviePath);
                }
            }
        }
    }//GEN-LAST:event_jlMovieListMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private LPlayerUILib.JImageButton btnDel;
    private javax.swing.JList jlMovieList;
    private LPlayerUILib.JImagePanel plButtons;
    private javax.swing.JScrollPane spTreePane;
    // End of variables declaration//GEN-END:variables
}
